package com.hzzc.core.service;

import com.hzzc.BizException;
import com.hzzc.core.dao.SSORoleMapper;
import com.hzzc.core.model.SSORole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * @author zmm
 * 角色管理服务
 */
@Service
public class RoleService {

    @Autowired
    private SSORoleMapper ssoRoleMapper;


    /**
     * 获取应用所有的角色
     *
     * @param appId
     * @return 当应用id为null时，忽略app的条件限制
     */
    public List<SSORole> getAppRole(Long appId) {
        Example example = new Example(SSORole.class);
        example.orderBy("id").asc();
        example.createCriteria().andEqualTo("applicationId", appId);
        return ssoRoleMapper.selectByExample(example);
    }

    /**
     * 增加新用户
     *
     * @param name
     * @param name
     * @param appId
     * @param msg
     * @throws BizException
     */
    public void addRole(String name, Long appId, String msg) throws BizException {
        SSORole ssoRole = new SSORole();
        ssoRole.setApplicationId(appId);
        ssoRole.setName(name);
        ssoRole.setMsg(msg);
        ssoRoleMapper.insert(ssoRole);
    }

    /**
     * 更具Id删除角色
     *
     * @param roleId
     */
    public void delRole(Long roleId) {
        ssoRoleMapper.deleteByPrimaryKey(roleId);
    }

    /**
     * 更具Id获取角色
     *
     * @param roleIds
     * @return
     */
    public List<SSORole> selectByIds(Set<Long> roleIds) {
        Example example = new Example(SSORole.class);
        example.createCriteria().andIn("id", roleIds);
        return ssoRoleMapper.selectByExample(example);
    }
}
